<script setup lang="ts">
import { useAuth } from './composables/useAuth';
import { useHvacService } from './composables/useHvacService';
import { ROOM_IDS, MAX_SERVICE_OBJECTS, WAIT_TIME } from './constants/index';
import Login from './components/Login.vue';
import Homepage from './components/Homepage.vue';
import MainLayout from './components/MainLayout.vue';
import RoomView from './components/views/RoomView.vue';
import AdminView from './components/views/AdminView.vue';
import FrontDeskView from './components/views/FrontDeskView.vue';
import ManagerView from './components/views/ManagerView.vue';

// 使用认证管理
const {
  currentUser,
  userName,
  currentView,
  handleLogin,
  handleLogout,
  hasPermission,
  switchView
} = useAuth();

// 使用服务管理
const {
  hvacService,
  refreshKey,
  allRooms,
  serviceQueue,
  waitingQueue,
  allBills,
  occupiedRooms,
  checkInRecords
} = useHvacService();

// 刷新处理
const handleRefresh = () => {
  refreshKey.value++;
};
</script>

<template>
  <!-- 首页/登录页面 -->
  <Homepage 
    v-if="!currentUser" 
    :on-login="handleLogin"
    :total-rooms="ROOM_IDS.length"
    :max-service-objects="MAX_SERVICE_OBJECTS"
    :serving-count="serviceQueue.length"
    :waiting-count="waitingQueue.length"
  />

  <!-- 主应用 -->
  <MainLayout
    v-else
    :user-name="userName"
    :current-view="currentView"
    :total-rooms="ROOM_IDS.length"
    :max-service-objects="MAX_SERVICE_OBJECTS"
    :wait-time="WAIT_TIME"
    :serving-count="serviceQueue.length"
    :waiting-count="waitingQueue.length"
    :has-permission="hasPermission"
    :switch-view="switchView"
    :on-logout="handleLogout"
  >
    <!-- 客房控制视图 -->
    <RoomView 
      v-if="currentView === 'room'"
      :hvac-service="hvacService"
      :occupied-rooms="occupiedRooms"
      :all-rooms="allRooms"
      :refresh-key="refreshKey"
      @refresh="handleRefresh"
    />

    <!-- 管理员监控视图 -->
    <AdminView 
      v-if="currentView === 'admin'"
      :hvac-service="hvacService"
      :service-queue="serviceQueue"
      :waiting-queue="waitingQueue"
      :all-rooms="allRooms"
      :max-service-objects="MAX_SERVICE_OBJECTS"
      @refresh="handleRefresh"
    />

    <!-- 前台结账视图 -->
    <FrontDeskView 
      v-if="currentView === 'frontdesk'"
      :hvac-service="hvacService"
      :available-rooms="ROOM_IDS"
      :occupied-rooms="occupiedRooms"
      :check-in-records="checkInRecords"
      :all-rooms="allRooms"
      :all-bills="allBills"
      @refresh="handleRefresh"
    />

    <!-- 统计报表视图 -->
    <ManagerView 
      v-if="currentView === 'manager'"
      :hvac-service="hvacService"
    />
  </MainLayout>
</template>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
    'Noto Sans', sans-serif;
  background: #f8f9fb;
}

#app {
  width: 100%;
  min-height: 100vh;
}
</style>
